<%@ page import="DB.*" %>
<%@ page import="POJO.*" %>
<%@ page import="java.util.Vector" %>
<%@ page import ="java.io.File" %>


<%
            ContentContainerManager ccm = new ContentContainerManager(DBInfo.getConnection());

            if (request.getParameter("containerid") != null && !ccm.containerIsViewableByUser(Integer.parseInt(request.getParameter("containerid")), ((User) session.getAttribute("user")).username)) {%>
<h1 class="nice">You do not have the rights to view this page</h1>
<h2 class="nice">Or, this page does not exist</h2>
<% } else {
    ContentManager cm = new ContentManager(DBInfo.getConnection());
    CommentManager cmmm = new CommentManager(DBInfo.getConnection());

    int containerid = Integer.parseInt(request.getParameter("containerid"));
    boolean userIsOwner = ccm.containerIsOwnedByUser(containerid, ((User) session.getAttribute("user")).username);

    if (request.getParameter("removeContentId") != null && userIsOwner) {
        String type = cm.getContentType(Integer.parseInt(request.getParameter("removeContentId")));
        if (type != null && type.compareTo("IMAGE") == 0) {
            File temp = new File("/opt/apache-tomcat-6.0.16/webapps/gwtgroep8.didactiek1.edm.uhasselt.be/web/" + cm.getContentLink(Integer.parseInt(request.getParameter("removeContentId"))));
            temp.delete();
        }
        cm.removeObject(Integer.parseInt(request.getParameter("removeContentId")));
    }

    ContainerObject co = ccm.getContainerLocation(containerid);
    TripManager tm = new TripManager(DBInfo.getConnection());
%>
<div class="nice"><h1><% out.print(co.location + " (<a class=\"nice\" href=\"index.jsp?page=trip&amp;tripid=" + co.tripID + "\">" + tm.getTripName(co.tripID) + "</a>)");%></h1>
    &nbsp;&nbsp;&nbsp;<span class="b">Coordinates:</span>&nbsp; <%out.print(co.north);%>&deg; North, <%out.print(co.east);%>&deg; East<br />
&nbsp;&nbsp;&nbsp;<span class="b">Arrival:</span>&nbsp; <%out.print(co.arrival);%></div>

<script type="text/javascript">    
    function showComments(id)
    {
        $.post("comments", {contentid: "" + id + "", content: "true"}, function(data){
            if(data.length > 0) {
                $("#commentscontent" + id).hide(1000, function () {});   
                $("#commentscontent" + id).html(data + "<br />");
                $("#commentscontent" + id).show(1000, function () {});    
            }
        });
            
    }  
    
    function showCommentsOnComment(id, contid)
    {
        $.post("comments", {contentid: "" + contid + "", commentid: "" + id + ""}, function(data){
            if(data.length > 0) {
                $("#commentscomment" + id).hide(500, function () {});   
                $("#commentscomment" + id).html(data);
                $("#commentscomment" + id).show(500, function () {});    
            }
        });
            
    }  

    function hideComments(id)
    {
        $("#commentscontent" + id).hide(1000, function () {});        
    }  
    
    function hideCommentsOnComment(id)
    {
        $("#commentscomment" + id).hide(500, function () {});        
    }  
    
    function changeComment(conteid, comid, contaid, t)
    {
        $.post("comments", {contentid: "" + conteid + "", commentid: "" + comid + "", containerid: "" + contaid + "", text: "" + t + "", content: "true"}, function(data){
            if(data.length > 0) {
                showComments(conteid);
            }            
        });
    }
    
    function changeCommentOnComment(conteid, comid, comid2, contaid, t)
    {
        $.post("comments", {contentid: "" + conteid + "", commentid: "" + comid + "", commentidchange: "" + comid2 + "", containerid: "" + contaid + "", text: "" + t + ""}, function(data){
            if(data.length > 0) {
                showCommentsOnComment(comid, conteid);
            }            
        });
    }
    
    function deleteComment(conteid, comid, contaid)
    {
        $.post("comments", {contentid: "" + conteid + "", commentid: "" + comid + "", containerid: "" + contaid + "", del: "ok", content: "true"}, function(data){
            if(data.length > 0) {
                showComments(conteid);
            }            
        });
    }
    
    function deleteCommentOnComment(conteid, comid,comid2, contaid)
    {
        $.post("comments", {contentid: "" + conteid + "", commentid: "" + comid + "", commentiddelete: "" + comid2 + "", containerid: "" + contaid + "", del: "ok"}, function(data){
            if(data.length > 0) {
                showCommentsOnComment(comid, conteid);
            }            
        });
    }
    
    function addComment(conteid, contaid, t)
    {
        $.post("comments", {contentid: "" + conteid + "", containerid: "" + contaid + "", text: "" + t + "", add: "ok", content: "true"}, function(data){
            if(data.length > 0) {
                showComments(conteid);
            }            
        });
    }
    
    function addCommentOnComment(conteid, comid, contaid, t)
    {
        $.post("comments", {contentid: "" + conteid + "",commentid: "" + comid + "", containerid: "" + contaid + "", text: "" + t + "", add: "ok"}, function(data){
            if(data.length > 0) {
                showCommentsOnComment(comid, conteid);
            }            
        });
    }
    
    function checkYouTube(form) {
        var link = form.ytlink.value;
        if (link.match(/^http:\/\/\w*.?youtube.com\/watch\?v=\w+$/) != link) {
            alert("YouTube link be as follows http://something.youtube.com/watch?v=VIDEOID !");
            form.ytlink.focus();
            return false;
        }
        else
            return true;
    }
    
    function showNewBlog(containerid, tripid)
    {
        $.post("addBlogPost", {tripid: "" + tripid + "", containerid: "" + containerid + ""}, function(data){
            if(data.length > 0) {
                $("#newblog").hide(100, function () {});    
                $("#newblog").html(data);
                $("#newblog").show(1000, function () {});
            }            
        });
       
    }         
</script>

<% Vector<ContentObject> blogposts = cm.getBlogposts(containerid);
    if (blogposts.size() > 0 || userIsOwner) {%>
<div class="nice" style="text-align:left;">
    <h2>Blog posts</h2>
    <% for (int i = 0; i < blogposts.size(); i++) {
        if (userIsOwner) {
    %>
    <div style="text-align:center;">
        <form action="index.jsp?page=addContent&amp;type=editblogpost&amp;containerid=<% out.print(containerid);%>&amp;elementid=<%out.print(blogposts.elementAt(i).id);%>" method="post">
            <table style="width:100%"><tr><td colspan="1">
                <textarea cols="65" rows="20" name="blogpost"><%out.print(blogposts.elementAt(i).content);%></textarea></td></tr>
                <tr style="text-align:center;"><td style="text-align:center;"><input type="submit" value="Edit" /></td></tr>
                <tr style="text-align:right;"><td style="text-align:right;"><%out.print(" <a class=\"nice\" href=\"index.jsp?page=triplocation&amp;containerid=" + containerid + "&amp;removeContentId=" + blogposts.elementAt(i).id + "\">[X]</a>");%></td></tr>
            </table>
    </form></div>
    <%
    } else {%>
    <div style="text-align:center;"><textarea readonly="readonly" cols="65" rows="20"><%out.print(blogposts.elementAt(i).content);%></textarea></div>
    <% }
        out.print("<div style=\"text-align:right;\">Comments: <span class=\"anice\" onclick=\"showComments(" + blogposts.elementAt(i).id + ");\">show</span>/<span class=\"anice\" onclick=\"hideComments(" + blogposts.elementAt(i).id + ");\">hide</span></div><div style=\"margin-top:5px; width:100%; text-align:center;\" class=\"comments\" id=\"commentscontent" + blogposts.elementAt(i).id + "\"></div>\n");
    }
    if (userIsOwner) {
        ContentContainerManager contentm = new ContentContainerManager(DBInfo.getConnection());
        int tripid = contentm.containerBelongsToTrip(containerid);
    %>
    <span class="anice" onclick="showNewBlog(<% out.print(containerid);%>, <% out.print(tripid);%>);">Add a new blog post</span>
    <div id="newblog"></div>    
    <% }%>
</div>
<%}%>

<% Vector<ContentObject> images = cm.getImages(containerid);
    if (images.size() > 0 || userIsOwner) {%>
<div class="nice" style="text-align:left;">
    <h2>Pictures</h2>
    <div style="width:100%; text-align:center;">
        <% for (int i = 0; i < images.size(); i++) {
        out.print("<table style=\"width:100%\"><tr><td style=\"text-align:center;\"><a href=\"" + images.elementAt(i).content + "\"><img width=\"640px\" src=\"" + images.elementAt(i).content + "\" alt=\"image\"></img></a></td></tr>");
        if (userIsOwner) {
            out.print("<tr><td style=\"text-align:right;\"><a class=\"nice\" href=\"index.jsp?page=triplocation&amp;containerid=" + containerid + "&amp;removeContentId=" + images.elementAt(i).id + "\">[X]</a></td></tr>");
        }
        out.print("<tr><td style=\"text-align:right;\">Comments: <span class=\"anice\" onclick=\"showComments(" + images.elementAt(i).id + ");\">show</span>/<span class=\"anice\" onclick=\"hideComments(" + images.elementAt(i).id + ");\">hide</span><div style=\"margin-top:5px; width:100%; text-align:center;\" class=\"comments\" id=\"commentscontent" + images.elementAt(i).id + "\"></div></td></tr></table>");

    }
    if (userIsOwner) {%>
        <form enctype="multipart/form-data" action="index.jsp?page=addContent&amp;type=image&amp;containerid=<% out.print(containerid);%>" method="post">
            <table style="width:100%; text-align:center;">
                <tr><td style="width:100%;"><%out.print("To add a picture, browse for the wanted file, and click submit:\n");%></td></tr>
                <tr><td style="width:100%;"><input size="30" type="file" name="filename"  /></td></tr>                
                <tr><td ><input type="submit" value="Submit" /></td></tr>
            </table>
        </form>
        <%  }%>
</div></div>
<% }%>


<% Vector<ContentObject> videos = cm.getVideos(containerid);
    if (videos.size() > 0 || userIsOwner) {%>
<div class="nice" style="text-align:left;">
    <h2>Videos</h2>
    <div style="width:100%; text-align:center;">
        <% for (int i = 0; i < videos.size(); i++) {%>
        
        <table style="width:100%"><tr><td>
                <% if (videos.elementAt(i).content.startsWith("http")) // Video is YouTube link
     {
         out.print("\n<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"" + videos.elementAt(i).content + "\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"" + videos.elementAt(i).content + "\" type=\"application/x-shockwave-flash\" style=\"width:425px; height:344px\"></embed></object>");
     } else { /*uploaded video*/%>
            
                <object type="application/x-shockwave-flash" width="425" height="344"
                        
                        wmode="transparent" data="http://gwtgroep8.didactiek1.edm.uhasselt.be:8080/flashplayer/mediaplayer.swf?file=http://gwtgroep8.didactiek1.edm.uhasselt.be:8080/web/uploads/<%out.print(videos.elementAt(i).content);%>&autoStart=true">
                    
                    <param name="movie" value="http://gwtgroep8.didactiek1.edm.uhasselt.be:8080/flashplayer/mediaplayer.swf?file=http://gwtgroep8.didactiek1.edm.uhasselt.be:8080/web/uploads/<%out.print(videos.elementAt(i).content);%>&autoStart=true" />
                    
                    <param name="wmode" value="transparent" />
                    
                </object>
                
                
                <% }%>
        </td></tr>
        <tr><td style="text-align:right;">
                <%  if (userIsOwner) {
         out.print(" <a class=\"nice\" href=\"index.jsp?page=triplocation&amp;containerid=" + containerid + "&amp;removeContentId=" + videos.elementAt(i).id + "\">[X]</a>");
        %></td></tr>
        <tr><td style="text-align:right;"><%        }
        out.print("<div style=\"text-align:right;\">Comments: <span class=\"anice\" onclick=\"showComments(" + videos.elementAt(i).id + ");\">show</span>/<span class=\"anice\" onclick=\"hideComments(" + videos.elementAt(i).id + ");\">hide</span></div><div style=\"margin-top:5px; width:100%; text-align:center;\" class=\"comments\" id=\"commentscontent" + videos.elementAt(i).id + "\"></div>\n</td></tr></table>");
    }%>
        <%if (userIsOwner) {%>
        <div style="width:100%; text-align:center;">            
            <form action="index.jsp?page=addContent&amp;type=video&amp;containerid=<% out.print(containerid);%>" method="post" onsubmit="return checkYouTube(this);">
                <table style="width:100%; text-align:center;">
                    <tr><td style="width:100%;"><%out.print("To add a video, insert the YouTube link below, and click submit:\n");%></td></tr>
                    <tr><td><input type="text" name="ytlink" size="50" value="http://www.youtube.com/watch?v=[VIDEOID]" />&nbsp;<input type="submit" value="Submit" /></td></tr>
                </table>
            </form>
            <form enctype="multipart/form-data" action="index.jsp?page=addContent&amp;type=video&amp;containerid=<% out.print(containerid);%>" method="post">
                <table style="width:100%; text-align:center;">
                    <tr><td style="width:100%;"><%out.print("To upload a video, browse for the wanted file, and click submit:\n");%></td></tr>
                    <tr><td style="width:100%;"><input size="30" type="file" name="videofilename"  /></td></tr>                
                    <tr><td ><input type="submit" value="Submit" /></td></tr>
                </table>
            </form>
        </div>
        <% }
        %>
</div></div>
<% }%>

<% if (videos.size() < 1 && images.size() < 1 && blogposts.size() < 1 && !userIsOwner) {%>
<div class="nice" style="text-align:left;">
    <h2>No content found.</h2>
</div>
<% }%>

<% }
// Voor de else aan het begin van dit document %>